package com.example.testrecorder;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.testrecorder.domain.QueAndAns;
import com.example.testrecorder.domain.User;
import com.example.testrecorder.utils.Des3Util;
import com.example.testrecorder.utils.MyDialog;
import com.example.testrecorder.utils.ViewUtil;

import org.litepal.LitePal;

import java.util.List;


public class FindPasswordActivity extends BaseActivity {
    private Toolbar toolbar;
    private EditText telEt,anEt;
    private LinearLayout qLl,aLl;
    private TextView quTv,qTv;
    private Button btn;
    private int currAn=0;//当前正在回答第几个问题
    private static final int FIRST=1;
    private static final int SECOND=2;
    private static final int THIRD=3;
    private QueAndAns queAndAns1;
    private QueAndAns queAndAns2;
    private QueAndAns queAndAns3;
    private User targetUser;
    private static final String TAG = "FindPasswordActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        initView();
    }

    private void initView() {
        toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        telEt=findViewById(R.id.telEt);
        anEt=findViewById(R.id.anEt);//答案输入框
        qLl=findViewById(R.id.qLl);//问题的线性布局
        aLl=findViewById(R.id.aLl);//答案的线性布局
        quTv=findViewById(R.id.quTv);//问题一
        qTv=findViewById(R.id.qTv);//我的姓名是
        btn=findViewById(R.id.btn);
        telEt.addTextChangedListener(new MyTextWatcher(telEt));
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(currAn==FIRST){//如果当前正在回答第一个问题
                    if(anEt.getText().toString().equals(queAndAns1.getAnswer())){//如果问题回答正确
                        currAn=SECOND;//进入下一个问题的回答
                        quTv.setText("问题二");
                        qTv.setText(queAndAns2.getQuestion());
                        anEt.setText("");
                    }else {
                        MyDialog.showInfo(FindPasswordActivity.this,"您的答案有误，请重新输入！");
                    }
                }else if(currAn==SECOND){//如果当前正在回答第二个问题
                    if(anEt.getText().toString().equals(queAndAns2.getAnswer())){//如果问题回答正确
                        currAn=THIRD;//进入下一个问题的回答
                        quTv.setText("问题三");
                        qTv.setText(queAndAns3.getQuestion());
                        anEt.setText("");
                        btn.setText("查看我的账号及密码");
                    }else {
                        MyDialog.showInfo(FindPasswordActivity.this,"您的答案有误，请重新输入！");
                    }
                }else if(currAn==THIRD){//如果当前正在回答第二个问题
                    if(anEt.getText().toString().equals(queAndAns3.getAnswer())){//如果问题回答正确，3个问题都回答正确，告知用户的账号、密码
                        String password = Des3Util.decrypt("lyy", targetUser.getPassword());//将密码进行解密
                        MyDialog myDialog=new MyDialog(FindPasswordActivity.this, Color.parseColor("#f4f8fb"));
                        myDialog.setContent("恭喜您，密码找回成功！您的账号是："+targetUser.getUserName()+"\n"+"密码是："+password);
                        myDialog.setPositiveText("马上登录");
                        myDialog.show();
                        myDialog.setOnNegativeListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                myDialog.dismiss();
                            }
                        });
                        myDialog.setOnPositiveListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(FindPasswordActivity.this,LoginActivity.class));//跳转至登录界面
                            }
                        });
                    }else {
                        MyDialog.showInfo(FindPasswordActivity.this,"您的答案有误，请重新输入！");
                        Log.d(TAG, "onClick:您的答案有误，请重新输入 ");
                        Log.d(TAG, "当前正在回答第几个问题： "+currAn);
                    }
                }else {
                    MyDialog.showInfo(FindPasswordActivity.this,"3个密保问题要全部回答正确才可以找回密码！");
                }

            }
        });
    }
    private class MyTextWatcher implements TextWatcher{
        private EditText editText;
        private int maxLen;//编辑框中内容的最大长度
        private CharSequence content;//编辑框中的内容

        public MyTextWatcher(EditText editText){//构造器
            this.editText=editText;
            maxLen= ViewUtil.getMaxLength(editText);//通过反射获取编辑框内容的最大长度
        };

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            content=s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(TextUtils.isEmpty(content)){
                return;
            }
            if(content.length()==11){
                ViewUtil.hideAllInputMethod(FindPasswordActivity.this);//隐藏输入法
                List<User> users = LitePal.where("tel=?", content.toString()).find(User.class);
                if(users.size()==1){//找到了号码的主人
                    targetUser = users.get(0);
                    List<QueAndAns> list = LitePal.where("user_id=?", "" + targetUser.getId()).find(QueAndAns.class);
                    if(list.size()==3){//如果用户设置有3个密保问题
                        qLl.setVisibility(View.VISIBLE);
                        aLl.setVisibility(View.VISIBLE);
                        btn.setVisibility(View.VISIBLE);
                        queAndAns1 = list.get(0);
                        queAndAns2 = list.get(1);
                        queAndAns3 = list.get(2);
                        qTv.setText(queAndAns1.getQuestion());
                        quTv.setText("问题一");
                        currAn=FIRST;//标志当前为第一个问题
                        anEt.requestFocus();
                        telEt.setEnabled(false);
                    }else if(list.size()==0){
                        MyDialog.showInfo(FindPasswordActivity.this,"抱歉，您未设置有密保问题，无法在此处找回密码！");
                    }else {
                        MyDialog.showInfo(FindPasswordActivity.this,"未知错误，请联系管理员！");
                    }
                } else if(users.size()==0){//没有找到号码的主人
                    MyDialog.showInfo(FindPasswordActivity.this,"数据库不存在该手机号,无法进入下一步，请确认您的账号已经设置绑定手机号或者核对后再重新输入！");
                }else {
                    MyDialog.showInfo(FindPasswordActivity.this,"未知错误，请联系管理员！");
                }

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}